/*=============================================================================*
 *  Copyright 2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *=============================================================================*/
package topics.util;

import topics.*;
import topics.impl.TopicImpl;
import topics.impl.TopicSpaceImpl;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.*;
import java.io.InputStream;

/**
 *
 * @author  Stefan Lischke
 */
public class TopicSpaceParser
   extends DefaultHandler
{
   private static TopicSpace      m_topicSpace;
   private static java.util.Stack stack;
   private int                    depth = 0;
   private String                 tns;

   /**
    * DOCUMENT_ME
    *
    * @param is DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public static TopicSpace parse( InputStream is )
   {
      // Use an instance of ourselves as the SAX event handler
      DefaultHandler handler = new TopicSpaceParser(  );

      // Parse the input with the default (non-validating) parser
      stack = new java.util.Stack(  );
      try
      {
         SAXParser saxParser = SAXParserFactory.newInstance(  ).newSAXParser(  );

         //System.out.println("start parsing");
         saxParser.parse( is, handler );

         //System.out.println("end parsing");
      }
      catch ( Exception e )
      {
         e.printStackTrace(  );
      }

      return m_topicSpace;
   }

   /**
    * DOCUMENT_ME
    *
    * @param namespaceURI DOCUMENT_ME
    * @param localName DOCUMENT_ME
    * @param qName DOCUMENT_ME
    *
    * @throws SAXException DOCUMENT_ME
    */
   public void endElement( String namespaceURI,
                           String localName, // local name
                           String qName ) // qualified name
   throws SAXException
   {
      if ( qName.endsWith( "Topic" ) )
      {
         if ( depth > 0 )
         {
            depth--;
            stack.pop(  );
         }
      }
   }

   /**
    * DOCUMENT_ME
    *
    * @param namespaceURI DOCUMENT_ME
    * @param localName DOCUMENT_ME
    * @param qName DOCUMENT_ME
    * @param attrs DOCUMENT_ME
    *
    * @throws SAXException DOCUMENT_ME
    */
   public void startElement( String     namespaceURI,
                             String     localName, // local name
                             String     qName, // qualified name
                             Attributes attrs )
   throws SAXException
   {
      try
      {
         //System.out.println("start element "+qName);
         if ( qName.endsWith( "TopicNamespace" ) )
         {
            tns             = attrs.getValue( "targetNamespace" );
            m_topicSpace    = new TopicSpaceImpl( tns );

            //System.out.println("topicspace "+tns);
         }

         if ( qName.endsWith( "Topic" ) )
         {
        	 String attrValue = attrs.getValue("name");
        	 
            Topic t = new TopicImpl( attrValue );
            if ( depth == 0 )
            {
               m_topicSpace.addTopic( t );
               stack.push( t );
            }
            else
            {
               Topic in = (Topic) stack.peek(  );
               in.addTopic( t );
               stack.push( t );
            }

            //System.out.println(depth+" : "+attrs.getValue("name"));
            depth++;
         }
      }
      catch ( Exception e )
      {
         e.printStackTrace(  );
      }
   }
}